<?php
/**
 * Created by PhpStorm.
 * User: jc234839
 * Date: 20/04/14
 * Time: 11:02 AM
 */
require 'init.php';

if (isset($_POST['submit'])) {
    if ($_REQUEST['submit'] == "Checkout" && isset($_SESSION['cart'])) {
        header('Location: cart.php');
        exit();
    }
    else if ($_REQUEST['submit'] == "Add To Cart")
    {

        if (empty($_POST['quantity']) === true || ctype_digit($_POST['quantity']) == false) {

            $message[] = 'Quantity can not be empty and can only contain a whole number';
        }
        else
        {
            if (!isset($_SESSION['cart'])) {
                $_SESSION['cart'] = array();
                $_SESSION['cart']['0'] = $_REQUEST;
                $_SESSION['totalItems'] += $_REQUEST['quantity'];

                header('Location: individual.php?productID=' . $_REQUEST['productID']);
                exit();
            }
            else
            {
                foreach ($_SESSION['cart'] as &$cartItem) {
                    if (($cartItem['productID'] == $_REQUEST['productID']) && ($cartItem['size'] == $_REQUEST['size'])) {
                        $cartItem['quantity'] += $_REQUEST['quantity'];
                        $_SESSION['totalItems'] += $_REQUEST['quantity'];

                        header('Location: individual.php?productID=' . $_REQUEST['productID']);
                        exit();
                    }
                }
                $_SESSION['cart'][count($_SESSION['cart'])] = $_REQUEST;
                $_SESSION['totalItems'] += $_REQUEST['quantity'];

                header('Location: individual.php?productID=' . $_REQUEST['productID']);
                exit();
            }
        }
    }
    else if ($_REQUEST['submit'] == "Continue Shopping") {
        header('Location: gallery.php');
        exit();
    }
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script type="text/javascript">
    </script>
    <title>Individual</title>
    <meta name="description" content="Awesome steampunk tshirt">
    <meta name="keywords" content="steampunk, tshirts, shopping">
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png" alt="Left Steampunk Cog"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png" alt="Right Steampunk Cog"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="contentFrame">
            <div id="featured">
                <?php
                $sql = "SELECT P.ProductID, P.ProductName, P.UnitPrice, P.ProductDescription, I.Path FROM Products AS P, Images AS I WHERE P.ProductID = I.ProductID AND P.ProductID = ?";
                $query = $db->prepare($sql);
                $query->bindValue(1, $_GET['productID']);

                try {
                    $query->execute();
                    if ($query) {
                        $record = $query->fetch();
                        ?>
                        <div class="individual">
                            <img src="<?php echo $record['Path']; ?>" height="427px" width="427px"
                                 alt="<?php echo $record['ProductName']; ?>"/>
                        </div>
                        <div class="orderFormContainer">
                            <form method="post" action="" name="orderForm" id="orderForm">

                                <input type="hidden" name="productID" id="productID"
                                       value="<?php echo $record['ProductID']; ?>">
                                <input type="hidden" name="name" id="name"
                                       value="<?php echo $record['ProductName']; ?>">
                                <input type="hidden" name="description" id="description"
                                       value="<?php echo $record['ProductDescription']; ?>">
                                <input type="hidden" name="price" id="price"
                                       value="<?php echo $record['UnitPrice']; ?>">
                                <table>
                                    <tr>
                                        <td><label for="size">Size:</label></td>
                                        <td>
                                            <select id="size" name="size">
                                                <option value="XS">X-Small</option>
                                                <option value="S">Small</option>
                                                <option value="M" selected="selected">Medium</option>
                                                <option value="L">Large</option>
                                                <option value="XL">X-Large</option>
                                            </select>
                                        </td>
                                        <td rowspan="2" style="vertical-align: middle"><input type="submit"
                                                                                              name="submit"
                                                                                              id="addToCartBtn"
                                                                                              value="Add To Cart"></td>
                                    </tr>
                                    <tr>
                                        <td><label for="quantity">Quantity:</label></td>
                                        <td><input type="text" name="quantity" id="quantity" size="9"/></td>
                                        <td></td>
                                    </tr>
                                </table>
                                <hr/>
                                <div id="controls">
                                    <input type="submit" name="submit" id="continueShoppingBtn"
                                           value="Continue Shopping">
                                    <input type="submit" name="submit" id="checkoutBtn" value="Checkout">
                                </div>
                                <hr/>
                            </form>
                            <div class="individualDescription">
                                <p><?php echo $record['ProductName'] . ' - $ ' . $record['UnitPrice']; ?></p>
                                <p><?php echo str_replace(".", ".</p><p>", $record['ProductDescription']); ?></p>
                                <p style="color: #333;">Model is wearing size small</p>
                                <p>
                                    All steamy Tees are made from 100% Cotton for that perfect slouch because lets face it who wears t-shirts that fit.Steamy Tees prides itself on providing the highest-grade material ensuring all our punks leave with a, well punks don’t smile.
                                </p>
                            </div>
                        </div>
                        <div class="ribbon">
                            <p><?php echo $record['ProductName'] . ' - $ ' . $record['UnitPrice']; ?></p>
                        </div>
                        <div id="statusBar">
                            <?php
                            if (empty($message) === false) {
                                echo "<script type='text/javascript'>document.getElementById('statusBar').style.display = 'block';</script>";
                                echo '<p>' . implode('<p></p>', $message) . '</p>';
                            }
                            ?>
                        </div>
                    <?php
                    } else {
                        echo "<p>Product does not exist</p>";
                    }
                } catch (PDOException $e) {
                    echo $e->getMessage();
                }
                ?>
            </div>
        </div>
    </div>
    <?php require "footer.php" ?>
</div>
</body>
</html>